package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.ShippingDAO;
import com.xsmart.oa.dao.UserAddressDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户常用地址
 *
 * @author 郭建德
 */
@Service
public class UserAddressService {
    @Autowired
    UserAddressDao userAddressDao;

    /**
     * 查询地区信息
     *
     * @param requestBean 请求对象
     *                    rid：父级地区id
     *                    rtype：地区类型，0国家，1省级，2地市，3区县
     * @return
     */
    public ResponseBean getAreaList(RequestBean requestBean) {
        Integer rid = requestBean.getRequestParamInteger("rid");
        Integer rtype = requestBean.getRequestParamInteger("rtype");
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.addResultItem("list", userAddressDao.getAreaList(rid, rtype));
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询用户常用地址
     *
     * @param requestBean
     * @return
     */
    public ResponseBean queryUserAddress(RequestBean requestBean) {
        Integer uid = requestBean.getRequestParamInteger("uid");
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.addResultItem("adds", userAddressDao.queryUserAddress(uid));
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询用户默认地址
     *
     * @param requestBean
     * @return
     */
    public ResponseBean querydefaultAddress(RequestBean requestBean) {
        Integer uid = requestBean.getRequestParamInteger("uid");
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.addResultItem("adds", userAddressDao.querydefaultAddress(uid));
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
    /**
     * 设置常用地址
     *
     * @param requestBean
     * @return
     */
    public ResponseBean setDefault(RequestBean requestBean) {
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer addrId = requestBean.getRequestParamInteger("addrId");
        ResponseBean responseBean = requestBean.getResponseBean();
        //取消默认
        userAddressDao.setDefault(uid, null, 0);
        //设置默认
        userAddressDao.setDefault(uid, addrId, 1);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 新增用户常用地址
     *
     * @param requestBean adds: 详细地址
     *                    city: 地市
     *                    code: 编码
     *                    quyu: 区县
     *                    receiver: 收货人
     *                    sheng: 省
     *                    tel: 电话
     *                    user_id: 用户id
     *                    shengName: 省名称
     *                    shiName: 市名称
     *                    quyuName: 区名称
     * @return 添加结果
     */
    public ResponseBean addUserAddress(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String adds = requestBean.getRequestParamString("adds");
        Integer country = requestBean.getRequestParamInteger("country");
        Integer city = requestBean.getRequestParamInteger("city");
        Integer province = requestBean.getRequestParamInteger("sheng");
        Integer code = requestBean.getRequestParamInteger("code");
        Integer district = requestBean.getRequestParamInteger("quyu");
        String receiver = requestBean.getRequestParamString("receiver");
        String tel = requestBean.getRequestParamString("tel");
        String provinceName = requestBean.getRequestParamString("shengName");
        String cityName = requestBean.getRequestParamString("shiName");
        String districtName = requestBean.getRequestParamString("quyuName");
        Integer uid = requestBean.getRequestParamInteger("user_id");
        Map<String, Object> userAddressMap = new HashMap<>();
        userAddressMap.put("user_id", uid);
        userAddressMap.put("consignee", receiver);
        userAddressMap.put("email", "无");
        userAddressMap.put("country", country);
        userAddressMap.put("province", province);
        userAddressMap.put("city", city);
        userAddressMap.put("district", district);
        userAddressMap.put("street", "0");
        userAddressMap.put("address", adds);
        if(district == null){
            userAddressMap.put("full_address", provinceName + " " + cityName + " " + adds);
        }else {
            userAddressMap.put("full_address", provinceName + " " + cityName + " " + districtName + " " + adds);
        }
        userAddressMap.put("zipcode", code);
        userAddressMap.put("tel", tel);
        userAddressMap.put("mobile", tel);
        userAddressMap.put("sign_building", "无");
        userAddressMap.put("best_time", "无");
        userAddressMap.put("create_time", new Date());
        userAddressMap.put("last_mod_time", new Date());
        int num = userAddressDao.addUserAddress(userAddressMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改用户常用地址
     *
     * @param requestBean
     * @return
     */
    public ResponseBean modUserAddress(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer addrId = requestBean.getRequestParamInteger("addrId");
        String adds = requestBean.getRequestParamString("adds");
        Integer country = requestBean.getRequestParamInteger("country");
        Integer city = requestBean.getRequestParamInteger("city");
        Integer province = requestBean.getRequestParamInteger("sheng");
        Integer code = requestBean.getRequestParamInteger("code");
        Integer district = requestBean.getRequestParamInteger("quyu");
        String provinceName = requestBean.getRequestParamString("shengName");
        String cityName = requestBean.getRequestParamString("shiName");
        String districtName = requestBean.getRequestParamString("quyuName");
        String receiver = requestBean.getRequestParamString("receiver");
        String tel = requestBean.getRequestParamString("tel");
        Map<String, Object> userAddressMap = new HashMap<>();
        userAddressMap.put("user_id", uid);
        userAddressMap.put("addressId", addrId);
        userAddressMap.put("consignee", receiver);
        userAddressMap.put("email", "无");
        userAddressMap.put("country", country);
        userAddressMap.put("province", province);
        userAddressMap.put("city", city);
        userAddressMap.put("district", district);
        userAddressMap.put("street", "0");
        userAddressMap.put("address", adds);
        userAddressMap.put("zipcode", code);
        userAddressMap.put("tel", tel);
        userAddressMap.put("mobile", tel);
        userAddressMap.put("sign_building", "无");
        userAddressMap.put("best_time", "无");
        userAddressMap.put("create_time", new Date());
        userAddressMap.put("last_mod_time", new Date());
        if(district == null){
            userAddressMap.put("full_address", provinceName + " " + cityName + " " + adds);
        }else {
            userAddressMap.put("full_address", provinceName + " " + cityName + " " + districtName + " " + adds);
        }
        int num = userAddressDao.modUserAddress(userAddressMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 删除用户常用地址
     *
     * @param requestBean
     * @return
     */
    public ResponseBean delUserAddress(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer addrId = requestBean.getRequestParamInteger("addrId");
        int num = userAddressDao.delUserAddress(uid, addrId);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
}
